package com.firefighting.maintenancer;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.firefighting.entity.Contract;
import com.firefighting.util.JdbcUtils;
import com.firefighting.util.MYJSONObject;

import net.sf.json.JSONArray;

/**
 * Servlet implementation class GetMaintenancePlan
 */
@WebServlet(asyncSupported = true, urlPatterns = { "/GetMaintenancePlan" })
public class GetMaintenancePlan extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html");
		response.setCharacterEncoding("utf-8");
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		System.out.println("-----"+format.format(new Date())+"    "+this.getServletName()+"-----");
		

		String userName=request.getParameter("userName");

		PrintWriter out = response.getWriter();
		MYJSONObject result_jb = new MYJSONObject();
		
		Connection connection = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
	//	ResultSetMetaData data =null;
		try {
			connection = JdbcUtils.getConnection();
			String sql = "select contract.Id,contract.project_name,contract.sign_date from maintenanceplan,contract,users,task where users.userName=? and "
					+ "contract.Id=maintenanceplan.contract_id and task.app_id_account=users.userId and task.maintenanceplan_id=maintenanceplan.id "
					+ "group by contract.Id,contract.project_name,contract.sign_date";
			ps = connection.prepareStatement(sql);
			ps.setString(1, userName);
			//ps.setString(2, password);
			rs = ps.executeQuery();
			// data=rs.getMetaData();
		//	System.out.print(data.getColumnCount());
			// System.out.print(data.getColumnName(1));
			
			ArrayList<Contract> contract_array = new ArrayList<>();
			while(rs.next())
			{
				Contract contract = new Contract();
				contract.setId(rs.getInt(1));
				contract.setProjectName(rs.getString(2));
				contract.setSignDate(rs.getDate(3));
				contract_array.add(contract);
				/*MYJSONObject temp = new MYJSONObject();
				MYJSONObject temp_1 = new MYJSONObject();
				JSONArray temp_ja = new JSONArray();
				temp.put("project_id", rs.getString("contract_id"));
				temp.put("project_name", rs.getString("project_name"));
				temp.put("start_time", rs.getString("sign_date"));
				temp_1.put("maintenanceplan_id", rs.getString("id"));
				temp_1.put("maintenanceplan_name", rs.getString("name"));
				temp_1.put("maintenanceplan_status", rs.getString("state_finished"));
				temp_ja.add(temp_1.JSON());
				temp.put("maintenanceplan", temp_ja);
				ja.add(temp.JSON());*/
			}
			JSONArray contract_ja = new JSONArray();
			for (Contract contract : contract_array) {
				MYJSONObject contract_jb = new MYJSONObject();
				contract_jb.put("project_id", contract.getId());
				contract_jb.put("project_name", contract.getProjectName());
				contract_jb.put("start_time", format.format(contract.getSignDate()));
				JSONArray maintenanceplan_ja = new JSONArray();
				
				ps.close();
				rs.close();
				sql = "select  DISTINCT maintenanceplan.id,maintenanceplan.plan_name,maintenanceplan.state_finished from maintenanceplan,contract,users,task where users.userName=?  and contract.id=? and contract.id=maintenanceplan.contract_id  " + 
						"and task.app_id_account=users.userId and task.maintenanceplan_id=maintenanceplan.id ";
				ps = connection.prepareStatement(sql);
				ps.setString(1, userName);
				ps.setInt(2, contract.getId());
				rs = ps.executeQuery();
				//ArrayList<Maintenanceplan> maintenanceplan_array = new ArrayList<>();
				while (rs.next()) {
					MYJSONObject maintenance_jb=new MYJSONObject();
					/*Maintenanceplan maintenanceplan = new Maintenanceplan();
					maintenanceplan.setId(rs.getInt(1));
					maintenanceplan.setName(rs.getString(2));
					maintenanceplan.setState(rs.getString(3));
					maintenanceplan_array.add(maintenanceplan);*/
					maintenance_jb.put("maintenanceplan_id", rs.getInt(1));
					maintenance_jb.put("maintenanceplan_name", rs.getString(2));
					maintenance_jb.put("maintenanceplan_status", rs.getString(3));
					maintenanceplan_ja.add(maintenance_jb.toString());
				}
				contract_jb.put("maintenanceplan", maintenanceplan_ja);
				contract_ja.add(contract_jb.toString());
			//	contract.setMaintenanceplan_array(maintenanceplan_array);
			}
			
			/*for (Contract contract : contract_array) {
				MYJSONObject contract_jb = new MYJSONObject();
				contract_jb.put("project_id", contract.getId());
				contract_jb.put("project_name", contract.getProjectName());
				contract_jb.put("start_time", simpleDateFormat.format(contract.getSignDate()));
				JSONArray maintenanceplan_ja = new JSONArray();
				for (Maintenanceplan maintenanceplan : contract.getMaintenanceplan_array()) {
					MYJSONObject maintenanceplan_jb = new MYJSONObject();
					maintenanceplan_jb.put("maintenanceplan_id", maintenanceplan.getId());
					maintenanceplan_jb.put("maintenanceplan_name", maintenanceplan.getName());
					maintenanceplan_jb.put("maintenanceplan_status", maintenanceplan.getState());
					maintenanceplan_ja.add(maintenanceplan_jb.toString());
				}
				contract_jb.put("maintenanceplan", maintenance_ja);
				contract_ja.add(contract_jb.toString());
			}*/
			
			result_jb.put("status", "true");
			result_jb.put("plan", contract_ja);
		} catch (SQLException e) {
			e.printStackTrace();
			result_jb.put("status", "false");
			result_jb.put("message", "服务器异常");
		} finally {
			JdbcUtils.free(rs, ps, connection);
			out.print(result_jb.toString());
			out.flush();
			out.close();
		}
	}

}
